layui.config({
    base: '../../layui/' //此处路径请自行处理, 可以使用绝对路径
}).extend({
    formSelects: 'formSelects-v4'
}).use(['jquery', 'formSelects', 'form','layer'], function () {
    var formSelects = layui.formSelects
        , form = layui.form
        , layer = layui.layer
        , $ = layui.jquery;

    formSelects.on('select1', function (id, vals, val, isAdd, isDisabled) {

    }, true);
    formSelects.data('tableName', 'server', {
        url: '/table/all',
        beforeSuccess: function (id, url, searchVal, result) {
            //我要把数据外层的code, msg, data去掉
            result = result.data;
            //然后返回数据
            return result;
        },
        success: function (id, url, searchVal, result) {
        }
    });
    form.render();

    form.on('submit(add)', function (data) {
        var module = "";
        $("input:checkbox[name='module']:checked").each(function () {
            module = $(this).val() + "," + module;
        });
        if (module.endsWith(",", module.length)) {
            module = module.substring(0, module.length - 1)
        } else {
            alert("请选择生成的模块");
            return false;
        }
        data.field.module = module;
        var method = "";
        $("input:checkbox[name='method']:checked").each(function () {
            method = $(this).val() + "," + method;
        });
        if (method.endsWith(",", method.length)) {
            method = method.substring(0, method.length - 1)
        }
        data.field.method = method;
        var params = {
            absolutePathApi: data.field.absolutePathApi,
            contentPathApi: data.field.contentPathApi,
            absolutePathWeb: data.field.absolutePathWeb,
            contentPathWeb: data.field.contentPathWeb,
            author: data.field.author,
            tableName: data.field.tableName,
            module: data.field.module,
            method: data.field.method
        }
        submit($, params);
        return false;

    });

    function submit($, params) {
        $.ajax({
            url: '/table/add',
            data: JSON.stringify(params),
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            type: "POST",
            success: function (result) {
                progressDisplay();
            }
        })
    }
    function progressDisplay() {
        layer.open({
            type: 2,
            title: '自动生成代码进度',
            area: ['500px', '300px'],
            fix: false, //
            closeBtn: true,
            shift: 2,
            shadeClose: false,
            content: ["progressDisplay.html", 'no'],
            success: function (layero, index) {
                form.render();
            },
            yes: function () {
            }
        });
    }
});

//关闭页面
function CloseWin() {
    parent.location.reload(); // 父页面刷新
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index); //再执行关闭
}
